/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package elaborate.tag_analysis.client.actions;

import elaborate.tag_analysis.client.actions.ui.MultipleFileSelectionDialog;
import elaborate.tag_analysis.client.actions.ui.multiplefileselector.MultipleFileSelectionConfEntry;
import elaborate.tag_analysis.model.Resource;
import elaborate.tag_analysis.model.TURRelation;
import elaborate.tag_analysis.model.Tag;
import elaborate.tag_analysis.restws.client.ResourceDao;
import elaborate.tag_analysis.restws.client.TURRelationDao;
import elaborate.tag_analysis.restws.client.TagDao;
import elaborate.task.Task;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author lendle
 */
public class GenerateResourceListActionHandler extends BaseActionHandler {
    @Override
    protected void asyncHandleAction(ActionEvent event) {
        try {
            List<MultipleFileSelectionConfEntry> entries=new ArrayList<MultipleFileSelectionConfEntry>();
            
            MultipleFileSelectionConfEntry vectorFileEntry=new MultipleFileSelectionConfEntry();
            vectorFileEntry.setId("file");
            vectorFileEntry.setDisplayName("Select Output File");
            entries.add(vectorFileEntry);
            
            final MultipleFileSelectionDialog dlg=new MultipleFileSelectionDialog((Frame)context.getMainFrame(), true);
            dlg.getFileSelector().setMultipleFileSelectionConfEntries(entries);
            dlg.setLocationRelativeTo(context.getMainFrame());
            dlg.setVisible(true);
            
            if(dlg.isOk()){
                context.executeTask(new Task(){

                    @Override
                    public Object run() throws Exception {
                        File file=dlg.getFileSelector().getSelectedFile("file");
                        ResourceDao resourceDao = context.getActiveApplication().getDaoFactory().createResourceDao();
       
                        List<Resource> resources = resourceDao.findAll(context.getActiveApplication().getApplicationId());
                        PrintWriter output = null;
                        try {
                            output = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), "utf-8"));
                            boolean first=true;
                            for (Resource resource : resources) {
                                if(!first){
                                    output.println();
                                }
                                first=false;
                                output.print(resource.getId());
                            }
                        } finally {
                            if (output != null) {
                                output.close();
                            }
                        }
                        return null;
                    }
                });
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
